package cn.arikeky.product.service.impl;

import cn.arikeky.basic.util.BaiduAuditUtils;
import cn.arikeky.pet.domain.Pet;
import cn.arikeky.product.domain.Product;
import cn.arikeky.product.domain.ProductDetail;
import cn.arikeky.product.domain.ProductOnlineAuditLog;
import cn.arikeky.product.mapper.ProductMapper;
import cn.arikeky.product.mapper.ProductOnlineAuditLogMapper;
import cn.arikeky.product.service.IProductDetailService;
import cn.arikeky.product.service.IProductOnlineAuditLogService;
import cn.arikeky.product.service.IProductService;
import cn.arikeky.basic.service.impl.BaseServiceImpl;
import com.sun.mail.imap.protocol.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author arikeky
 * @since 2022-11-03
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private IProductDetailService productDetailService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void add(Product product) {

        product.setState(0L);
        super.add(product);
        ProductDetail productDetail = null;
        if (product.getDetail() == null) {
            productDetail = new ProductDetail();
        } else {
            productDetail = product.getDetail();
        }
        productDetail.setProductId(product.getId());
        productDetailService.add(productDetail);
    }

    @Override
    public void onsale(List<Long> ids) {
       // 过滤掉上架状态的数据,换为map，key就是主键，value就是对象
        List<Product> productList = productMapper.findAll().stream().filter(pdt -> pdt.getState() == 0).collect(Collectors.toList());
        Map<Long, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, e -> e));
        Date date = new Date();
        for (Long id : ids){
            Product product = productMap.get(id);
            StringBuilder sbr = new StringBuilder();
            if (product != null) {
                if (!BaiduAuditUtils.TextCensor(product.getName())){
                    sbr.append("宠物服务名称百度AI审核失败， 服务名称为：" + product.getName() + "!");
                }
                if (product.getResources().split(";").length > 1) {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources().split(",")[0])){
                        sbr.append("宠物服务上架百度AI审核图片失败，图片地址为：" + product.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(product.getResources())){
                        sbr.append("宠物服务上架百度AI审核图片失败，图片地址为：" + product.getResources() + ";");
                    }
                }

                if (sbr.length() > 0) {
                    ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                    log.setAuditTime(date);
                    log.setProductId(id);
                    log.setNote(sbr.toString());
                    log.setState(0);
                    productOnlineAuditLogMapper.add(log);
                }
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditTime(date);
                log.setProductId(id);
                log.setNote("审核通过");
                log.setState(1);
                productOnlineAuditLogMapper.add(log);
                product.setState(1L);
                product.setOnsaletime(date);
                productMapper.update(product);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        // 如果要判断，全部查询出来，只要状态是下架那么就不处理
        productMapper.batchOffsale(ids);
    }

    @Override
    public Product getProductDetail(Long id) {
        return productMapper.getProductDetail(id);
    }
}
